<%@page import="com.mto.module.karaoke.service.CategoryLocalServiceUtil"%>
<%@page import="com.mto.module.karaoke.model.Category"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@ include file="/jsps/main.jsp"%>

<%
String orderByCol = ParamUtil.getString(request, "orderByCol", "songName");
String orderByType = ParamUtil.getString(request, "orderByType", "asc");
String 
	songName = ParamUtil.getString(request, "songName", StringPool.BLANK), 
	songAuthor = ParamUtil.getString(request, "songAuthor", StringPool.BLANK), 
	songSinger = ParamUtil.getString(request, "songSinger", StringPool.BLANK), 
	recordNumber = ParamUtil.getString(request, "recordNumber", StringPool.BLANK);
Integer songCategory = ParamUtil.getInteger(request, "songCategory", 0);

Song searchSong = new SongImpl();
searchSong.setRecordNumber(-1);
if(!songAuthor.isEmpty()) searchSong.setSongAuthor(StringPool.PERCENT + songAuthor + StringPool.PERCENT);
if(songCategory > 0) searchSong.setSongCategory(songCategory);
if(!songName.isEmpty()) searchSong.setSongName(StringPool.PERCENT + songName + StringPool.PERCENT);
if(!songSinger.isEmpty()) searchSong.setSongSinger(StringPool.PERCENT + songSinger + StringPool.PERCENT);

List<Category> categories = CategoryLocalServiceUtil.getCategories(0, CategoryLocalServiceUtil.getCategoriesCount());

%>

<aui:form action="<%=renderResponse.createRenderURL() %>">
	<aui:layout cssClass="portlet-layout">
		<aui:column cssClass="aui-w20 portlet-column portlet-column-first">
			<div class="portlet-column-content portlet-column-content-first">
				<ul class="karaoke-menu">
					<portlet:renderURL var="addUrl">
						<portlet:param name="jspPage" value="/jsps/quan-tri/update.jsp" />
					</portlet:renderURL>
					<li><a href="<%=addUrl%>"><liferay-ui:message key="tao-bai-hat"/></a></li>
				</ul>
			</div>
	
		</aui:column>
		<aui:column cssClass="aui-w80 portlet-column portlet-column-last">
			<div class="portlet-column-content portlet-column-content-last">
				<%
				PortletURL renderUrl = renderResponse.createRenderURL();
				PortletURL redirectUrl = renderResponse.createRenderURL();
				PortletURL actionUrl = renderResponse.createActionURL();
				%>
				<div id="karaoke-list">
				<liferay-ui:search-container emptyResultsMessage="no-song-found" delta="<%=10 %>" orderByCol="<%=orderByCol %>" orderByType="<%=orderByType %>">
					<liferay-ui:search-container-results>
						<%
						results = SongServiceUtil.search(searchSong, searchContainer.getStart(), searchContainer.getEnd(), orderByCol, orderByType);
						if(results == null) results = new ArrayList<Song>();
						else results = ListUtil.copy(results);
						total = SongServiceUtil.searchCount(searchSong);

						results.add(0, searchSong);
						
						pageContext.setAttribute("results", results);
						pageContext.setAttribute("total", total);
						%>
					</liferay-ui:search-container-results>
					
					<liferay-ui:search-container-row className="com.mto.module.karaoke.model.Song" modelVar="song" keyProperty="songId">
						<%
						boolean isSearchRow = (song.getSongId() == 0);
						String songId = isSearchRow?StringPool.BLANK:String.valueOf(song.getSongId());
						renderUrl.setParameter("songId", songId);
						actionUrl.setParameter("songId", songId);
						%>
						
						<liferay-ui:search-container-column-text name='songId' value="<%= songId %>" align="center" orderable="<%=true %>" />
						
						<liferay-ui:search-container-column-text name='songName' orderable="<%=true %>" orderableProperty="songName">
							<c:choose>
								<c:when test="<%= isSearchRow %>">
									<input type="text" name="<%=ns%>songName" value="<%=songName%>" class="input-100">
								</c:when>
								<c:otherwise><%=song.getSongName() %></c:otherwise>
							</c:choose>
						</liferay-ui:search-container-column-text>
						
						<liferay-ui:search-container-column-text name="songAuthor" orderable="<%=true %>" orderableProperty="songAuthor">
							<c:choose>
								<c:when test="<%= isSearchRow %>">
									<input type="text" name="<%=ns%>songAuthor" value="<%=songAuthor%>" class="input-100">
								</c:when>
								<c:otherwise><%=song.getSongAuthor() %></c:otherwise>
							</c:choose>
						</liferay-ui:search-container-column-text>
						
						<liferay-ui:search-container-column-text name="songCategory" orderable="<%=true %>" orderableProperty="songCategory">
							<c:choose>
								<c:when test="<%= isSearchRow %>">
									<aui:select name="songCategory" label="" inputCssClass="selectbox">
										<aui:option value="">---</aui:option>
										<% for(Category category : categories) { %>
										<aui:option value="<%=category.getCategoryId()%>"><c:out value="<%=category.getName() %>"/></aui:option>
										<% } %>
									</aui:select>
								</c:when>
								<c:otherwise>
									<%
									Category category = CategoryLocalServiceUtil.getCategory(song.getSongCategory());
									%>
									<%=category.getName() %>
								</c:otherwise>
							</c:choose>
						</liferay-ui:search-container-column-text>
						
						<liferay-ui:search-container-column-text name="songSinger" orderable="<%=true %>" orderableProperty="songSinger">
							<c:choose>
								<c:when test="<%= isSearchRow %>">
									<input type="text" name="<%=ns%>songSinger" value="<%=songSinger%>" class="input-100">
								</c:when>
								<c:otherwise><%=song.getSongSinger() %></c:otherwise>
							</c:choose>
						</liferay-ui:search-container-column-text>
						
						<liferay-ui:search-container-column-text name="recordNumber" orderable="<%=true %>" orderableProperty="recordNumber" align="center">
							<c:choose>
								<c:when test="<%= isSearchRow %>">
									<input type="text" name="<%=ns%>recordNumber" value="<%=recordNumber%>" class="input-100">
								</c:when>
								<c:otherwise><%=song.getRecordNumber() %></c:otherwise>
							</c:choose>
						</liferay-ui:search-container-column-text>
						
						<liferay-ui:search-container-column-text align="center" cssClass="w-75px">
							<c:choose>
								<c:when test="<%= isSearchRow %>">
									<aui:button value="search" type="submit"/>
								</c:when>
								<c:otherwise>
									<liferay-ui:icon-menu message="" cssClass="float-none" >
										<%
										renderUrl.setParameter("jspPage", "/jsps/quan-tri/view.jsp");
										%>
										<liferay-ui:icon image="view" url="<%=renderUrl.toString() %>" />
										<%
										renderUrl.setParameter("jspPage", "/jsps/quan-tri/update.jsp");
										%>
										<liferay-ui:icon image="edit" url="<%=renderUrl.toString() %>" />
										<%
										actionUrl.setParameter("redirectUrl", redirectUrl.toString());
										actionUrl.setParameter(ActionRequest.ACTION_NAME, "deleteSong");
										%>
										<liferay-ui:icon image="delete" url="<%=actionUrl.toString() %>" />
									</liferay-ui:icon-menu>
								</c:otherwise>
							</c:choose>
						</liferay-ui:search-container-column-text>
					</liferay-ui:search-container-row>
					<liferay-ui:search-iterator />
				</liferay-ui:search-container>
				</div>
			</div>
		</aui:column>
	</aui:layout>
</aui:form>